/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shattered2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;

/**
 *
 * @author 10135228
 */
public class PiecesWindow extends JFrame implements ActionListener, KeyListener {
    
    JButton[] buttons = null;
    int buttonCounter =  0;
    PuzzleField fieldData = null;
    PuzzlePiece[] pieces = null;
    JPanel content = null;
    PiecesWindow mother = null;
    JProgressBar progress;
    JButton clearButton;
    int currentX = 0, currentY = 0;

    public PiecesWindow(String title,PuzzleField data,PuzzlePiece[] pieces) {
        super(title);
        this.fieldData = data;
        this.pieces = pieces;
        this.getContentPane().setLayout(new BorderLayout());
        content = new JPanel();
        
        content.setLayout(new FlowLayout());
        JScrollPane myScroller = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        content.setPreferredSize(new Dimension(1240,30000));
        this.getContentPane().add(myScroller,BorderLayout.CENTER);
        progress = new JProgressBar(JProgressBar.HORIZONTAL, 0, pieces.length-1);
        progress.setStringPainted(true);
        progress.setVisible(false);
        this.getContentPane().add(progress,BorderLayout.SOUTH);
        clearButton = new JButton("clear piece");
        clearButton.setActionCommand("clear");
        clearButton.addActionListener(this);
        this.getContentPane().add(clearButton, BorderLayout.NORTH);
        mother = this;
        this.addKeyListener(this);
       
    }

    void updatePieces(final int x, final int y) {
        
        buttonCounter = -1;
        this.currentX = x;
        this.currentY = y;
        new Thread(){

            @Override
            public void run() {
                content.setVisible(false);
                clearButton.setEnabled(false);
                content.removeAll();
                ArrayList<PiecesButton> buttonList = new ArrayList<>();
                
                int direction = 0;
                Integer[] borderColors = null;
                
                String topCode = null, leftCode = null, bottomCode = null, rightCode = null;
                if(fieldData.getPieceAt(x, y-1)!=null){
                    topCode = fieldData.getPieceAt(x, y-1).getBottomCode();
                    direction = PiecesButton.TOP;
                    borderColors = getBorderColors(fieldData.getPieceAt(x,y-1),PiecesButton.BOTTOM);
                }
                if(fieldData.getPieceAt(x-1, y)!=null){
                    leftCode = fieldData.getPieceAt(x-1, y).getRightCode();
                    if(direction == 0) {
                        direction = PiecesButton.LEFT;
                        borderColors = getBorderColors(fieldData.getPieceAt(x-1,y),PiecesButton.RIGHT);
                    }                    
                }
                if(fieldData.getPieceAt(x, y+1)!=null){
                    bottomCode = fieldData.getPieceAt(x, y+1).getTopCode();
                    if(direction == 0) {
                        direction = PiecesButton.BOTTOM;
                        borderColors = getBorderColors(fieldData.getPieceAt(x,y+1),PiecesButton.TOP);
                    }
                }
                if(fieldData.getPieceAt(x+1, y)!=null){
                    rightCode = fieldData.getPieceAt(x+1, y).getLeftCode();
                    if(direction == 0) {
                        direction = PiecesButton.RIGHT;
                        borderColors = getBorderColors(fieldData.getPieceAt(x+1,y),PiecesButton.LEFT);
                    }
                }
//                System.out.println("Top: "+topCode);
//                System.out.println("Left: "+leftCode);
//                System.out.println("Bottom: "+bottomCode);
//                System.out.println("Right: "+rightCode);
                
                int count = 0;
                
                progress.setVisible(true);
                progress.setMaximum(pieces.length);
                for (int i = 0; i < pieces.length; i++) {
                    progress.setValue(i);
                    if(pieces[i].topCodeEquals(topCode) &&
                        pieces[i].leftCodeEquals(leftCode) &&
                        pieces[i].bottomCodeEquals(bottomCode) &&
                        pieces[i].rightCodeEquals(rightCode) &&
                        !fieldData.isUsed(pieces[i].getID())){
                        count++;
                        PiecesButton myButton = new PiecesButton(pieces[i],borderColors,direction);
                        myButton.setActionCommand(Integer.toString(i));
                        myButton.addActionListener(mother);
                        myButton.setToolTipText("#"+myButton.getActionCommand());
                        buttonList.add(myButton);
                        

                        //mother.validate();
                        //mother.repaint();
                    }

                }
                buttons = buttonList.toArray(new PiecesButton[0]);

                Arrays.sort(buttons);
                progress.setMaximum(buttons.length);
                for (int j = 0; j < buttons.length; j++) {
                    content.add(buttons[j]);              
                    progress.setValue(j);
                }
                //mother.setPreferredSize(new Dimension(1240,ButtonHeight*((count/20)+1)));

                progress.setVisible(false);
                content.setVisible(true);
                clearButton.setEnabled(true);
                mother.setTitle("Pieces ("+count+")");
                mother.validate();
                mother.repaint();
                mother.requestFocus();
               
            }
           
        }.start();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = ((JButton)e.getSource()).getActionCommand();
        if(cmd.equals("clear")){
            fieldData.setPieceAt(this.currentX, this.currentY, null);
            this.updatePieces(currentX, currentY);
        }else{
            fieldData.setPieceAt(this.currentX, this.currentY, pieces[Integer.decode(cmd)]);
            this.setTitle("Piece #: "+cmd);
        }
        this.requestFocus();
    }
    
    static Integer[] getBorderColors(PuzzlePiece piece, int dir) {
            Integer[] colors = new Integer[14];
            int x = piece.getOrigin().x;
            int y = piece.getOrigin().y;
            BufferedImage image = piece.getImage();
            String code ="";
            if(dir==PiecesButton.TOP){
                code = piece.getTopCode();
                colors[0] = image.getRGB(x+2, y+2);
                colors[1] = image.getRGB(x+3, y+2);
                colors[2] = image.getRGB(x+4, y+2);
                if(code.charAt(0)=='1'){
                    colors[3] = image.getRGB(x+5, y);
                    colors[4] = image.getRGB(x+6, y);
                }else{
                    colors[3] = image.getRGB(x+5, y+4);
                    colors[4] = image.getRGB(x+6, y+4);
                }
                colors[5] = image.getRGB(x+7, y+2);
                if(code.charAt(1)=='1'){
                    colors[6] = image.getRGB(x+8, y);
                    colors[7] = image.getRGB(x+9, y);
                }else{
                    colors[6] = image.getRGB(x+8, y+4);
                    colors[7] = image.getRGB(x+9, y+4);
                }                
                colors[8] = image.getRGB(x+10, y+2);
                if(code.charAt(2)=='1'){
                    colors[9] = image.getRGB(x+11, y);
                    colors[10] = image.getRGB(x+12, y);
                }else{
                    colors[9] = image.getRGB(x+11, y+4);
                    colors[10] = image.getRGB(x+12, y+4);
                }                
                colors[11] = image.getRGB(x+13, y+2);
                colors[12] = image.getRGB(x+14, y+2);
                colors[13] = image.getRGB(x+15, y+2);
                
                return colors;
            }
            if(dir==PiecesButton.LEFT){
                code = piece.getLeftCode();
                colors[0] = image.getRGB(x+2, y+2);
                colors[1] = image.getRGB(x+2, y+3);
                colors[2] = image.getRGB(x+2, y+4);
                if(code.charAt(0)=='1'){
                    colors[3] = image.getRGB(x, y+5);
                    colors[4] = image.getRGB(x, y+6);
                }else{
                    colors[3] = image.getRGB(x+4, y+5);
                    colors[4] = image.getRGB(x+4, y+6);
                }
                colors[5] = image.getRGB(x+2, y+7);
                if(code.charAt(1)=='1'){
                    colors[6] = image.getRGB(x, y+8);
                    colors[7] = image.getRGB(x, y+9);
                }else{
                    colors[6] = image.getRGB(x+4, y+8);
                    colors[7] = image.getRGB(x+4, y+9);
                }                
                colors[8] = image.getRGB(x+2, y+10);
                if(code.charAt(2)=='1'){
                    colors[9] = image.getRGB(x, y+11);
                    colors[10] = image.getRGB(x, y+12);
                }else{
                    colors[9] = image.getRGB(x+4, y+11);
                    colors[10] = image.getRGB(x+4, y+12);
                }                
                colors[11] = image.getRGB(x+2, y+13);
                colors[12] = image.getRGB(x+2, y+14);
                colors[13] = image.getRGB(x+2, y+15);                
                return colors;
            }            
            if(dir==PiecesButton.BOTTOM){
                code = piece.getBottomCode();
                colors[0] = image.getRGB(x+2, y+15);
                colors[1] = image.getRGB(x+3, y+15);
                colors[2] = image.getRGB(x+4, y+15);
                if(code.charAt(0)=='1'){
                    colors[3] = image.getRGB(x+5, y+13);
                    colors[4] = image.getRGB(x+6, y+13);
                }else{
                    colors[3] = image.getRGB(x+5, y+17);
                    colors[4] = image.getRGB(x+6, y+17);
                }
                colors[5] = image.getRGB(x+7, y+15);
                if(code.charAt(1)=='1'){
                    colors[6] = image.getRGB(x+8, y+13);
                    colors[7] = image.getRGB(x+9, y+13);
                }else{
                    colors[6] = image.getRGB(x+8, y+17);
                    colors[7] = image.getRGB(x+9, y+17);
                }                
                colors[8] = image.getRGB(x+10, y+15);
                if(code.charAt(2)=='1'){
                    colors[9] = image.getRGB(x+11, y+13);
                    colors[10] = image.getRGB(x+12, y+13);
                }else{
                    colors[9] = image.getRGB(x+11, y+17);
                    colors[10] = image.getRGB(x+12, y+17);
                }                
                colors[11] = image.getRGB(x+13, y+15);
                colors[12] = image.getRGB(x+14, y+15);
                colors[13] = image.getRGB(x+15, y+15);
                return colors;
            }            
            if(dir==PiecesButton.RIGHT){
                code = piece.getRightCode();
                colors[0] = image.getRGB(x+15, y+2);
                colors[1] = image.getRGB(x+15, y+3);
                colors[2] = image.getRGB(x+15, y+4);
                if(code.charAt(0)=='1'){
                    colors[3] = image.getRGB(x+13, y+5);
                    colors[4] = image.getRGB(x+13, y+6);
                }else{
                    colors[3] = image.getRGB(x+17, y+5);
                    colors[4] = image.getRGB(x+17, y+6);
                }
                colors[5] = image.getRGB(x+15, y+7);
                if(code.charAt(1)=='1'){
                    colors[6] = image.getRGB(x+13, y+8);
                    colors[7] = image.getRGB(x+13, y+9);
                }else{
                    colors[6] = image.getRGB(x+17, y+8);
                    colors[7] = image.getRGB(x+17, y+9);
                }                
                colors[8] = image.getRGB(x+15, y+10);
                if(code.charAt(2)=='1'){
                    colors[9] = image.getRGB(x+13, y+11);
                    colors[10] = image.getRGB(x+13, y+12);
                }else{
                    colors[9] = image.getRGB(x+17, y+11);
                    colors[10] = image.getRGB(x+17, y+12);
                }                
                colors[11] = image.getRGB(x+15, y+13);
                colors[12] = image.getRGB(x+15, y+14);
                colors[13] = image.getRGB(x+15, y+15);                 
                return colors;
            }
            return null;
        }

@Override
            public void keyTyped(KeyEvent e) {
                char tmp = e.getKeyChar();

                if(!(tmp=='d' || tmp=='a' || tmp=='w' || tmp=='s'))
                    return;
                if(tmp=='d' && buttonCounter < 15900){
                    buttonCounter++;
                }else if(tmp=='a' && buttonCounter > 0){
                    buttonCounter--;
                }else if(tmp=='w' && buttonCounter > 19){
                    buttonCounter-=20;
                }else if(tmp=='s' && buttonCounter < 15880){
                    if(buttonCounter==-1){
                        buttonCounter++;
                    }
                    buttonCounter+=20;
                }
                if(buttonCounter> -1 && buttonCounter < buttons.length && buttons[buttonCounter]!=null){
                    buttons[buttonCounter].doClick();
                }else{
                    buttonCounter=-1;
                    JOptionPane.showMessageDialog(mother, "Reseting", "!", JOptionPane.WARNING_MESSAGE);
                }
                
              
            }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_SPACE)
            this.setExtendedState(JFrame.ICONIFIED);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        
    }

    
    
}
